﻿using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using Microsoft.Xna.Framework.Net;
using Microsoft.Xna.Framework.Storage;
using Krist_em_up.Characters;
using Krist_em_up.Characters.Enemies;
using Krist_em_up.Music;
using Krist_em_up.Components;
using Krist_em_up.Plateforms;
using Krist_em_up.Level;
using Krist_em_up.Interface;
using Krist_em_up.Item;
using Krist_em_up.Interface.Gui;

namespace Krist_em_up
{
    public class KristemupMulti : Kristemup
    {
        Kris deus;
        Viewport deusViewport;
        Vector2 origin2;
        public KristemupMulti(GameStateManager game,int level,int score)
            : base(game,level,score)
        {
            origin2 = Vector2.Zero;
        }
        public override void Initialize()
        {
            base.Initialize();

            tabKris = new Kris[2] { tabKris[0], null };
            deus = new Kris(this, tabKris[0].Position, game.settings.PlayerTwo,1);
            tabKris[1] = deus;
            game.graphics.ApplyChanges();
            gameViewport.Width /= 2;
            deusViewport = gameViewport;
            deusViewport.X = gameViewport.Width+1;
            gui = new GUI(this);
        }
        public override void LoadContent()
        {
            deus.LoadContent();
            base.LoadContent();
        }
        public override void Update(GameTime gameTime)
        {
            if (!stop)
            {
                origin2.X = deus.getPosition().X - 200;
                origin2.X = MathHelper.Clamp(origin2.X, 0, map.Plateforms.GetLength(0) * Plateform.Width - 800);
                Bonus[] tempB = new Bonus[bonus.Count];
                bonus.CopyTo(tempB);
                foreach (Bonus boost in tempB)
                {
                    if (boost.GetRec().Intersects(deus.GetRec()))
                    {
                        deus.bonus(boost, gameTime);
                        bonus.Remove(boost);
                    }
                }
            }
            base.Update(gameTime);
        }
        public override void Draw(GameTime gameTime)
        {
            game.GraphicsDevice.Viewport = deusViewport;

            paysage.Drawsol(spriteBatch,origin2);

            foreach (Enemy mob in Enemies)
            {
                if (mob.IsOnScreenE(origin2)) mob.Draw(gameTime, spriteBatch,origin2,stop);
            }

            foreach (Projectile pipiw in bullets)
            {
                pipiw.Draw(gameTime,origin2);
            }

            SpriteFont font = Content.Load<SpriteFont>("Fonts/Gamefont");
            spriteBatch.Begin();

            if (kris.reachEnd == true)
                spriteBatch.DrawString(font, "YOU FUCKING WON", new Vector2(game.graphics.PreferredBackBufferWidth / 2 - font.MeasureString("YOU FUCKING WON").X / 2,
                    game.graphics.PreferredBackBufferHeight / 2), Color.Red);


            for (int x = 0; x < map.Plateforms.GetLength(0); x++)
            {
                for (int y = 0; y < map.Plateforms.GetLength(1); y++)
                {
                    Texture2D texture = map.Plateform;
                    if (map.Plateforms[x, y].Collision == TileCollision.Impassable)
                    {
                        // Draw it in screen space.
                        Vector2 position = new Vector2(x, y) * Plateform.Size;
                        spriteBatch.Draw(texture, 
                            new Rectangle((int)(position - origin2).X, (int)(position - origin2).Y, Plateform.Width, Plateform.Height),
                            null, Color.White);
                    }
                }
            }
            spriteBatch.End();
            foreach (Bonus boost in bonus)
            {
                boost.DrawB(spriteBatch,origin2);
            }
            deus.Draw(gameTime,spriteBatch,origin2);
            kris.Draw(gameTime, spriteBatch, origin2);
            base.Draw(gameTime);
        }
    }
}
